﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xboxssbeatemup
{
    class BPlatformMovement : Behavior
    {
        public PlayerIndex player = PlayerIndex.One;
        public enum Controller
        {
            LeftThumb,
            RightThumb,
            DPad,
            KeyboardArrows,
            KeyboardNumPad
        }
        public Controller controller = Controller.LeftThumb;
        public float speed = 0.5f;
        GamePadState gamepadState;
#if !XBOX
        KeyboardState keyboardState;
#endif
        public Game game1;

        /// <summary>Construct BPlatformMovement Behavior</summary>
        /// <param name="game">The main Game object. You can just use "this".</param>
        /// <param name="t">Target GameObject that will receive this Behavior.</param>
        public BPlatformMovement(Game game, GameObject t) : base(game,t)
        {
            target = t;
            game1 = game;
        }

        /// <summary>Construct BPlatformMovement Behavior</summary>
        /// <param name="game">The main game object. You can just use "this".</param>
        /// <param name="t">Target GameObject that will receive this Behavior.</param>
        /// <param name="objects">Additional parameters: If of type PlayerIndex, specifies Player to trigger behavior. 
        /// If of type BPlatformMovement.Controller, specifies Controller that will move target GameObject.</param>
        public BPlatformMovement(Game game, GameObject t, params Object[] objects) : base(game, t)
        {
            foreach (Object o in objects)
            {
                if (o is PlayerIndex)
                {
                    player = (PlayerIndex)o;
                }
                if (o is Controller)
                {
                    controller = (Controller)o;
                }
                if (o is float)
                {
                    speed = (float)o;
                }
            }
        }


        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            gamepadState = GamePad.GetState(player);

            float leftRight = 0.0f;
            float upDown = 0.0f;
            switch (controller)
            {
                case Controller.LeftThumb:
                    leftRight = gamepadState.ThumbSticks.Left.X;
                    upDown = gamepadState.ThumbSticks.Left.Y;
                    break;
                case Controller.RightThumb:
                    leftRight = gamepadState.ThumbSticks.Right.X;
                    upDown = gamepadState.ThumbSticks.Right.Y;
                    break;
                case Controller.DPad:
                    leftRight = gamepadState.DPad.Right - gamepadState.DPad.Left;
                    upDown = gamepadState.DPad.Up - gamepadState.DPad.Down;
                    break;
                case Controller.KeyboardArrows:
#if !XBOX
                    keyboardState = Keyboard.GetState();
                    leftRight = (keyboardState.IsKeyDown(Keys.Left) && keyboardState.IsKeyUp(Keys.LeftControl) && keyboardState.IsKeyUp(Keys.LeftAlt) ? -1.0f : 0.0f) 
                        + (keyboardState.IsKeyDown(Keys.Right) && keyboardState.IsKeyUp(Keys.LeftControl) && keyboardState.IsKeyUp(Keys.LeftAlt) ? 1.0f : 0.0f);
                    upDown = (keyboardState.IsKeyDown(Keys.Down) && keyboardState.IsKeyUp(Keys.LeftControl) && keyboardState.IsKeyUp(Keys.LeftAlt) ? -1.0f : 0.0f)
                        + (keyboardState.IsKeyDown(Keys.Up) && keyboardState.IsKeyUp(Keys.LeftControl) && keyboardState.IsKeyUp(Keys.LeftAlt) ? 1.0f : 0.0f);
#endif
                    break;
                case Controller.KeyboardNumPad:
#if !XBOX
                    keyboardState = Keyboard.GetState();
                    leftRight = (keyboardState.IsKeyDown(Keys.NumPad4) ? -1.0f : 0.0f) + (keyboardState.IsKeyDown(Keys.NumPad6) ? 1.0f : 0.0f);
                    upDown = (keyboardState.IsKeyDown(Keys.NumPad2) ? -1.0f : 0.0f) + (keyboardState.IsKeyDown(Keys.NumPad8) ? 1.0f : 0.0f);
#endif
                    break;
            }

            this.target.position += Vector3.Multiply(new Vector3(leftRight, 0, -upDown),speed);

            if (leftRight > .01f || upDown > .01f
                || leftRight < -.01f || upDown < -.01f)
            {
                this.target.rotation = new Vector3(0.0f, (float)Math.Atan2(upDown, leftRight) - MathHelper.PiOver2, 0.0f);
            }

            

            base.Update(gameTime);
        }

    }
}
